package com.htht.job.admin.controller.shiro;

import com.htht.job.core.api.DubboShiroService;
import com.htht.job.core.biz.model.ReturnT;
import com.htht.job.executor.model.shiro.Resource;
import com.htht.job.executor.model.shiro.Role;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {


	@Autowired
	private DubboShiroService dubboShiroService;
	@RequestMapping
	public String index() {
		return "/shiro/role.index";
	}

	@RequestMapping("/pageList")
	@ResponseBody
	public String pageList(@RequestParam(required = false, defaultValue = "0") int start,
										@RequestParam(required = false, defaultValue = "10") int length,
										String searchText) {
		if (start != 0) {
			start = start / length;
		}
		return dubboShiroService.roleList(start, length, searchText);
	}
	@RequestMapping("/saveRole")
	@ResponseBody
	public ReturnT<String> saveRole(Role role){
		try {
			dubboShiroService.saveOrUpdateRole(role);
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnT.FAIL;
		}
	}
	@RequestMapping("/delRole/{id}")
	@ResponseBody
	public ReturnT<String> delRole(@PathVariable String id){
		try {
			dubboShiroService.deleteRole(id);
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnT.FAIL;
		}
	}
	@RequestMapping("/grant/{id}")
	@ResponseBody
	public ReturnT<String> grant(@PathVariable String id, @RequestParam(required = false) String[] resourceIds){
		try {
			dubboShiroService.grantResoure(id,resourceIds);
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnT.FAIL;
		}
	}


}
